<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Helper;

class ArrayHelper
{
    /**
     * 判断是否为关联数组.
     */
    public static function isAssociativeArray(array $items): bool
    {
        $i = 0;
        foreach (array_keys($items) as $value) {
            if (! is_int($value) || $value !== $i) {
                return true;
            }

            ++$i;
        }

        return false;
    }

    /**
     * 二维数组排序.
     *
     * @param array $array 数组
     * @param string $field 排序字段
     * @param int $sort 排序方式
     */
    public static function sort(array $array, string $field, int $sort = SORT_DESC): array
    {
        array_multisort(array_column($array, $field), $sort, $array);
        return $array;
    }
}
